Imports System
Imports System.Xml

''CONVERTED

Public Class TermsMsg
	Inherits BaseMsg

    'Protected m_secTyp As String
	'##Product Terms Request
	'<CPCAPI v="1.1">
	'      <ContractTermReq ReqID="testID" ProductID="LN"/>
	'</CPCAPI>

	'## Product Terms Response
	'<CPCAPI v="1.1">
	'      <ContractTermRsp ReqID="testID" ProductID="LN">
	'            <Term MMY="200706"/>
	'            <Term MMY="200707"/>

	Public Sub New()
        RequestType = "SecDefReq"
        ResponseType = "Batch"
        ReqTyp = "104" ' indicates futures. for options, should be 102
	End Sub

    'Public Sub SetReqTyp(ByVal cfi As String)
    '    If (cfi = "FUT") Then
    '        ReqTyp = "104"
    '    Else
    '        ReqTyp = "102"
    '    End If
    '    m_secTyp = cfi
    'End Sub

	Protected Overrides Function RequestMsg(ByRef XmlW As XmlTextWriter) As Boolean

        If m_prodid = "" Then
            Return False
        End If

        XmlW.WriteStartElement("Instrmt")
        XmlW.WriteAttributeString("ID", m_prodid)
        XmlW.WriteAttributeString("Src", "H")
        XmlW.WriteAttributeString("Exch", "NYMEX")

        'If (ReqTyp = "104") Then
        XmlW.WriteAttributeString("SecTyp", m_secTyp)
        'Else
        'XmlW.WriteAttributeString("SecTyp", "OOF")
        'End If
        XmlW.WriteEndElement()

        Return True
	End Function


    Protected Overrides Function InstrmtMsg(ByRef XmlW As XmlTextWriter) As Boolean

        If m_prodid = "" Then
            Return False
        End If

        XmlW.WriteStartElement("Instrmt")

        XmlW.WriteAttributeString("ID", m_prodid)
        XmlW.WriteAttributeString("Src", "H")
        XmlW.WriteAttributeString("Exch", "NYMEX")

        'If (ReqTyp = "104") Then
        XmlW.WriteAttributeString("SecTyp", m_secTyp)
        'Else
        'XmlW.WriteAttributeString("SecTyp", "OOF")
        'End If

        XmlW.WriteEndElement()
        Return True
    End Function

	Protected Overrides Sub ResponseParse(ByRef node As XmlNode)

		Dim prod As XmlNode
        'Dim attr As XmlAttribute

		Dim dbi As dbInterface = Globals.dbi

        'If node.Attributes("ProductID").InnerText = m_prodid Then

        dbi.nonquery("delete from nymx_prod_terms where prod_id=" & dbi.quote(m_prodid))
		'find the SecDef node, then find the instrmt node inside

		If node.Name = "SecDef" Then
			If node.Attributes("ReqRslt").InnerText = "0" Then
				InsertTerms(node)
			End If
		End If

		For Each prod In node.ChildNodes
			If prod.Name = "SecDef" Then
				If prod.Attributes("ReqRslt").InnerText = "0" Then
					InsertTerms(prod)
				End If
			End If
		Next
		'End If

	End Sub

	Private Sub InsertTerms(ByRef prod As XmlNode)
		'            <Term MMY="200706"/>
		Dim dbi As dbInterface = Globals.dbi
		Dim term As String
		Dim instr As XmlNode = prod
		Dim undly As XmlNode = Nothing
		'Dim child As XmlNode
        Dim matdt As String
        Dim mult As String
        Dim dtFirst As Date
        Dim dtLast As Date
        Dim dtLastEFP As Date
		Dim val As Date
		Dim undly_id As String = ""
		Dim undly_sectyp As String = ""
		Dim undly_mmy As String = ""

		For Each node As XmlNode In prod
			If node.Name = "Instrmt" Then
				instr = node
			End If
			If node.Name = "Undly" Then
				undly = node
			End If
		Next
		'instr = prod.ChildNodes(0) '  should be Instrmt node

		If instr Is Nothing Then
			Exit Sub
		End If

        term = getAttr(instr, "MMY")
        matdt = getAttr(instr, "MatDt")
        mult = getAttr(instr, "Mult")

		If Not undly Is Nothing Then
			undly_id = getAttr(undly, "ID")
			undly_sectyp = getAttr(undly, "SecTyp")
			undly_mmy = getAttr(undly, "MMY")
		End If

		If prod.Name = "Term" Then
			term = prod.Attributes("MMY").InnerText
        End If

        Dim chd As XmlNode
        Dim att As String

        For Each chd In instr.ChildNodes
            att = getAttr(chd, "EventTyp")
            val = Date.Parse(getAttr(chd, "Dt"))
            If att = "5" Then
                dtFirst = val
            ElseIf att = "7" Then
                dtLast = val
            ElseIf att = "105" Then
                dtLastEFP = val
            End If

        Next

        dbi.SetupStoredProc("sp_nymx_ins_product_term")
        dbi.AddStoredProcParam("@prod_id", SqlDbType.VarChar, m_prodid)
		dbi.AddStoredProcParam("@term", SqlDbType.VarChar, term)
		dbi.AddStoredProcParam("@undly_id", SqlDbType.VarChar, undly_id)
		dbi.AddStoredProcParam("@undly_sectyp", SqlDbType.VarChar, undly_sectyp)
		dbi.AddStoredProcParam("@undly_mmy", SqlDbType.VarChar, undly_mmy)
        'dbi.AddStoredProcParam("@maturity", SqlDbType.VarChar, matdt)
        'dbi.AddStoredProcParam("@multi", SqlDbType.VarChar, mult)
        'dbi.AddStoredProcParam("@dt_first_td", SqlDbType.DateTime, dtFirst)
        'dbi.AddStoredProcParam("@dt_last_td", SqlDbType.DateTime, dtLast)
        'dbi.AddStoredProcParam("@dt_last_efp", SqlDbType.DateTime, dtLastEFP)
        dbi.RunStoredProc()



    End Sub

End Class
